package com.whjfxx.terminal.biz.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.whjfxx.terminal.biz.enums.YesNoEnum;
import com.whjfxx.terminal.biz.mapper.PoAppMapper;
import com.whjfxx.terminal.biz.mapper.PoFuncMapper;
import com.whjfxx.terminal.biz.mapper.PoImportDataMapper;
import com.whjfxx.terminal.biz.param.ImportDataPageParam;
import com.whjfxx.terminal.biz.param.SearchParam;
import com.whjfxx.terminal.biz.param.TerminalParam;
import com.whjfxx.terminal.biz.service.PoAppService;
import com.whjfxx.terminal.biz.service.PoImportDataService;
import com.whjfxx.terminal.biz.service.PoTerminalService;
import com.whjfxx.terminal.biz.vo.ImportDataVo;
import com.whjfxx.terminal.entity.PoApp;
import com.whjfxx.terminal.entity.PoFunc;
import com.whjfxx.terminal.entity.PoImportData;
import com.whjfxx.terminal.exceptions.BizException;
import com.whjfxx.terminal.utils.BeanUtils;
import com.whjfxx.terminal.utils.PageUtils;
import com.whjfxx.terminal.utils.QueryUtil;
import org.apache.commons.collections4.CollectionUtils;
import org.hibernate.validator.constraints.Length;
import org.hibernate.validator.constraints.NotBlank;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.validation.constraints.NotNull;
import javax.validation.constraints.Pattern;
import java.util.Collections;
import java.util.List;
import java.util.Objects;

/**
 * Created by b.c on 2022/6/7 16:28
 */
@Service
public class PoImportDataServiceImpl extends ServiceImpl<PoImportDataMapper, PoImportData> implements PoImportDataService {

    @Autowired
    PoTerminalService poTerminalService;

    @Autowired
    PoAppMapper poAppMapper;

    @Autowired
    PoFuncMapper poFuncMapper;

    @Override
    public List<ImportDataVo> subList(String batchNo) {
        LambdaQueryWrapper<PoImportData> wrapper = new LambdaQueryWrapper();
        wrapper.eq(PoImportData::getBatchNo, batchNo);
        List<PoImportData> importDatas = this.list(wrapper);
        if (CollectionUtils.isEmpty(importDatas)) {
            return Collections.emptyList();
        }

        List<ImportDataVo> pageVo = BeanUtils.copyList(importDatas, ImportDataVo.class, (src, target) -> {
            ImportDataVo data = JSONObject.parseObject(src.getData(), ImportDataVo.class);
            BeanUtils.copyProperties(data, target);
            target.setBatchNo(src.getBatchNo());
            target.setDataStatus(src.getStatus());
            target.setBatchNo(src.getBatchNo());
            target.setMsg(src.getMsg());
        });
        return pageVo;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public PoImportData importData(PoImportData poImportData) {
        String data = poImportData.getData();
        ImportDataVo importDataVo = JSONObject.parseObject(data , ImportDataVo.class);

        PoApp app = poAppMapper.selectByAppName(importDataVo.getAppName());
        if (Objects.isNull(app)) {
            throw BizException.build("应用无效");
        }

        PoFunc func = poFuncMapper.selectByAppIdAndFuncName(app.getId(), importDataVo.getFuncName());
        if (Objects.isNull(func)) {
            throw BizException.build("功能无效");
        }

        TerminalParam param = new TerminalParam();
        param.setAppId(app.getId());
        param.setFuncId(func.getId());
        param.setSn(importDataVo.getSn());
        param.setTerminalType(importDataVo.getTerminalType());
        param.setExpiryType(importDataVo.getExpiryType());
        param.setPosition(importDataVo.getPosition());
        poTerminalService.insertSave(param, "import");

        poImportData.setStatus(YesNoEnum.YES.status);
        poImportData.setMsg("成功");
        this.updateById(poImportData);
        return poImportData;
    }
}
